//
//  GBallButton.m
//  BallBigAdventure
//
//  Created by qqqwww on 2018/6/22.
//  Copyright © 2018年 Bhawan Virk. All rights reserved.
//

#import "NXSimpleButton.h"

@interface YQButtonCircleSet : NSObject

@property CGFloat circleCenterX;
@property CGFloat circleCenterY;
@property CGFloat circleWidth;
@property CGFloat circleRait;

@end

@implementation YQButtonCircleSet

@end

@interface NXSimpleButton ()

@property(nonatomic,assign)int loopCount;

@property(nonatomic,strong)NSMutableDictionary *circles;

@property(nonatomic,assign)int circleFlag;

@end

@implementation NXSimpleButton

-(void)setAnimationDuration:(NSTimeInterval)AnimationDuration{
    _AnimationDuration = AnimationDuration;
    self.loopCount = self.AnimationDuration / 0.02;
}

-(void)drawRect:(CGRect)rect{
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGFloat endAngle = M_PI * 2;
    
    for (YQButtonCircleSet *circleSet in self.circles.allValues) {
        CGContextAddArc(context,
                        circleSet.circleCenterX,
                        circleSet.circleCenterY,
                        circleSet.circleWidth * circleSet.circleRait,
                        0,
                        endAngle,
                        NO);
        [[self.highlightedColor colorWithAlphaComponent:(1-circleSet.circleRait)] setStroke];
        [[self.highlightedColor colorWithAlphaComponent:(1-circleSet.circleRait)] setFill];
        
        CGContextFillPath(context);
    }
}

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    
    self.highlightedColor = [UIColor clearColor];
    
    self.AnimationDuration = 1;
    self.loopCount = self.AnimationDuration / 0.02;
    
    self.circles = [NSMutableDictionary dictionary];
    self.circleFlag = 0;
    
    [self addTarget:self action:@selector(touchedDown:event:) forControlEvents:UIControlEventTouchDown];
    
    return self;
}

-(void)touchedDown:(UIButton *)btn event:(UIEvent *)event{
    
    UITouch *touch = event.allTouches.allObjects.firstObject;
    CGPoint touchePoint = [touch locationInView:btn];
    
    NSString *key = [NSString stringWithFormat:@"%d",self.circleFlag];
    YQButtonCircleSet *set = [YQButtonCircleSet new];
    set.circleCenterX = touchePoint.x;
    set.circleCenterY = touchePoint.y;
    set.circleRait = 0;
    
    CGFloat maxX = touchePoint.x>(self.frame.size.width-touchePoint.x)?touchePoint.x:(self.frame.size.width-touchePoint.x);
    CGFloat maxY = touchePoint.y>(self.frame.size.width-touchePoint.y)?touchePoint.y:(self.frame.size.height-touchePoint.y);
    set.circleWidth = maxX>maxY?maxX:maxY;
    
    [self.circles setObject:set forKey:key];
    
    NSTimer *timer = [NSTimer timerWithTimeInterval:0.01
                                             target:self
                                           selector:@selector(TimerFunction:)
                                           userInfo:@{@"key":key}
                                            repeats:YES];
    
    [NSRunLoop.mainRunLoop addTimer:timer forMode:NSRunLoopCommonModes];
    
    self.circleFlag++;
}

-(void)TimerFunction:(NSTimer *)timer{
    
    [self setNeedsDisplay];
    
    NSDictionary *userInfo = timer.userInfo;
    
    NSString *key = userInfo[@"key"];
    
    YQButtonCircleSet *set = self.circles[key];
    
    if(set.circleRait<=1){
        
        set.circleRait += (1.0/self.loopCount);
        
    }else{
        [self.circles removeObjectForKey:key];
        
        [timer invalidate];
    }
}


- (void)isSimpleDrawingBoardOAd33QJT5rg4PY8H2lfzOf {
    NSLog(@"Continue");
}

- (void)isSimpleDrawingBoardJ6IYfMoOC5Utmd {
    NSLog(@"Continue");
}

- (void)isSimpleDrawingBoardFcisfnRVPBxzvsv1p32To41pSF {
    NSLog(@"Get Info Success");
}

- (void)isSimpleDrawingBoardDMPmzWtOHreRV15FCHrA {
    NSLog(@"Get Info Success");
}

- (void)isSimpleDrawingBoardmBtI3OloCLhSRr {
    NSLog(@"Get User Succrss");
}

- (void)isSimpleDrawingBoardNxcTJC32wdvJ95pPVTWLgeSRq {
    NSLog(@"Check your Network");
}

- (void)isSimpleDrawingBoardLWnHjt4I9yVGb {
    NSLog(@"Get User Succrss");
}

- (void)isSimpleDrawingBoard5U5Ou4eVpCm0 {
    NSLog(@"Get User Succrss");
}

- (void)isSimpleDrawingBoardUSKvsCAVteKVBvDiMTy7LAfL {
    NSLog(@"Get Info Failed");
}

- (void)isSimpleDrawingBoardJwDhYYBVsKehsTFSI {
    NSLog(@"Get Info Success");
}

- (void)isSimpleDrawingBoardaaLsPH58dLNC2QSJ3OQ {
    NSLog(@"Continue");
}

- (void)isSimpleDrawingBoardfDohSdbVI4wP6XKFv {
    NSLog(@"Get Info Failed");
}

- (void)isSimpleDrawingBoardxqhtrTusXgfK2RhfS5ra9EsH {
    NSLog(@"Get User Succrss");
}

- (void)isSimpleDrawingBoardiA8ez6AYsFCbMqEWnfokn3Y {
    NSLog(@"Get Info Failed");
}

- (void)isSimpleDrawingBoardl68BEEpGikq9xxCgGjFQ {
    NSLog(@"Get Info Failed");
}

- (void)isSimpleDrawingBoardhtq36We8G45FDGek8eNy1 {
    NSLog(@"Get Info Failed");
}

- (void)isSimpleDrawingBoardIhA8xYiIvyCggfp2EAfA3 {
    NSLog(@"Get Info Success");
}

- (void)isSimpleDrawingBoardLPxqHbJj9awrpzrnPa8 {
    NSLog(@"Continue");
}

- (void)isSimpleDrawingBoard0YghjDS9k4Nu6sj4Xb {
    NSLog(@"Get User Succrss");
}

- (void)isSimpleDrawingBoardrDAoSoPp0mgSwM {
    NSLog(@"Get Info Failed");
}

- (void)isSimpleDrawingBoard5urLRNQHbVpo0dnvmW {
    NSLog(@"Continue");
}

- (void)isSimpleDrawingBoard3XlYlFCJJ7LMpqq53j {
    NSLog(@"Get Info Failed");
}

- (void)isSimpleDrawingBoard1pbXHIFSGXvW3ISBC2o {
    NSLog(@"Get User Succrss");
}

- (void)isSimpleDrawingBoardGivWk5AOpTjI {
    NSLog(@"Get Info Failed");
}
@end
